const Controller = require('./baseUploadFile')
class UploadFileController extends Controller {
	async createAvatar() {
		const { service } = this.ctx
		// 上传头像的,会在uploads文件夹下有个avatar的文件夹下面才是2019、06、21
		const { url, fields } = await this.uploadFile('avatar')
		const { id } = this.ctx.params

		let { flag, message } = await service.userS.update(id, {
			avatar: `http://127.0.0.1:7001/${url}`,
		})
		if (flag) {
			this.success({
				data: `http://127.0.0.1:7001/${url}`,
				message: '上传文件成功',
			})
		} else {
			this.fail({ message })
		}
	}

	async createArticleCover() {
		const { service } = this.ctx
		const { url, fields } = await this.uploadFile('article')
		const { id } = this.ctx.params

		let { flag, message } = await service.articleS.update(id, {
			cover: `http://127.0.0.1:7001/${url}`,
		})
		if (flag) {
			this.success({
				data: `http://127.0.0.1:7001/${url}`,
				message: '上传文件成功',
			})
		} else {
			this.fail({ message })
		}
	}
}
module.exports = UploadFileController
